package com.neusoft.nepa.controller;

import com.neusoft.nepa.UserLogin;
import com.neusoft.nepa.entity.AdminData;
import javafx.application.Platform;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.List;

public class AQIConfigList {
    @FXML
    private void logOut(ActionEvent event) throws IOException {
        Stage stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();
        stage.getProperties().remove("userName");
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/neusoft/nepa/view/AdminLoginView.fxml"));
        Parent fxmlContent = fxmlLoader.load();
        BackgroundPane backgroundPane = new BackgroundPane();
        backgroundPane.getChildren().add(fxmlContent);
        fxmlContent.setStyle("-fx-background-color: transparent;");
        Scene newScene = new Scene(backgroundPane, 960, 540);
        stage.setTitle("NorthEast Software Corporation Public Supervisor Platform Manage System");
        stage.setScene(newScene);
        stage.show();
    }
        @FXML
        private TableView<AdminData> adminDataTableView;
        @FXML
        private TableColumn<AdminData, Integer> indexColumn;
        @FXML
        private TableColumn<AdminData, String> feedbackName;
        @FXML
        private TableColumn<AdminData, String> withinProvince;
        @FXML
        private TableColumn<AdminData, String> withinCity;
        @FXML
        private TableColumn<AdminData, String> previewIndex;
        @FXML
        private TableColumn<AdminData, String> feedbackDate;
        @FXML
        private TableColumn<AdminData, String> feedbackTime;
        @FXML
        private TableColumn<AdminData, String> operationHyperLink;
        @FXML
        private ObservableList<AdminData> dataList = FXCollections.observableArrayList();
        @FXML
        private void initialize() {
            System.out.println("执行了初始化方法");
            initColumn();
            // 重要：设置表格数据源（必须在添加数据前完成）
            adminDataTableView.setItems(dataList);
            Platform.runLater(() -> {
                try {
                    getInfoMap();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        }
        private void initColumn() {
            indexColumn.setCellValueFactory(cellData -> {
                int index = adminDataTableView.getItems().indexOf(cellData.getValue()) + 1;
                return new SimpleIntegerProperty(index).asObject();
            });

            // 反馈名称列（绑定 AdminData 的 feedbackName 属性）
            feedbackName.setCellValueFactory(cellData ->
                    cellData.getValue().feedbackNameProperty()
            );

            // 省内列（绑定 withinProvince 属性）
            withinProvince.setCellValueFactory(cellData ->
                    cellData.getValue().withinProvinceProperty()
            );

            // 市内列（绑定 withinCity 属性）
            withinCity.setCellValueFactory(cellData ->
                    cellData.getValue().withinCityProperty()
            );

            // 预演编号列（绑定 previewIndex 属性）
            previewIndex.setCellValueFactory(cellData ->
                    cellData.getValue().previewIndexProperty()
            );

            // 反馈日期列（绑定 feedbackDate 属性）
            feedbackDate.setCellValueFactory(cellData ->
                    cellData.getValue().feedbackDateProperty()
            );

            // 反馈时间列（绑定 feedbackTime 属性）
            feedbackTime.setCellValueFactory(cellData ->
                    cellData.getValue().feedbackTimeProperty()
            );

            operationHyperLink.setCellFactory(param -> new TableCell<>() {
                @Override
                protected void updateItem(String hyperlinkText, boolean empty) {
                    super.updateItem(hyperlinkText, empty);

                    if (empty || hyperlinkText == null) {
                        setGraphic(null);  // 无数据时不显示内容
                        return;
                    }

                    // 创建 Hyperlink 控件
                    Hyperlink hyperlink = new Hyperlink(hyperlinkText);
                    hyperlink.setStyle("-fx-text-fill: #0078d7; -fx-underline: true;");  // 蓝色超链接样式

                    // 绑定点击事件（示例：获取当前行数据并处理）
                    hyperlink.setOnAction(event -> {
                        // 获取当前行的 AdminData 对象
                        int rowIndex = getIndex();
                        AdminData currentData = adminDataTableView.getItems().get(rowIndex);

                        // 触发业务逻辑（例如打开详情页）
                        try {
                            handleHyperlinkClick(currentData);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    });

                    // 包裹 HBox 确保居中（可选）
                    HBox container = new HBox(hyperlink);
                    container.setStyle("-fx-alignment: center;");  // 内容居中
                    setGraphic(container);
                }
            });
            operationHyperLink.setCellValueFactory(cellData ->
                    cellData.getValue().hyperlinkProperty()  // 需确保 AdminData 有此属性
            );

        }
        private void getInfoMap() throws IOException {
            //Map<String(user name),String[](in the arrangement of ...)>
            List<String[]> arraylist = UserLogin.getInfo();
            for(String s[] : arraylist)
            {
                addInfo(new AdminData(s[5],s[0],s[1],s[2],s[3],s[4],s[6]));
            }


        }
        public void addInfo(AdminData a)
        {
            // 格式化为易读的字符串（注意 SimpleDateFormat 非线程安全）
            AdminData adminData = new AdminData(a.getFeedbackName(),a.getWithinProvince(),a.getWithinCity(),a.getPreviewIndex(),a.getFeedbackDate(), a.getFeedbackTime(),a.getInfo());
            adminData.setFeedbackName("wow");
            System.out.println("add:" + adminData);
            dataList.add(adminData);
        }
        public void changeToPublicSupervise(ActionEvent event) throws IOException {
            Stage stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/neusoft/nepa/view/PublicSUperviseList.fxml"));
            Parent parent = fxmlLoader.load();
            Scene newScene = new Scene(parent, 960, 540);
            stage.setTitle("NorthEast Software Corporation Public Supervisor Platform Manage System");
            stage.setScene(newScene);
            stage.show();
        }
    public void changeToProvinceGroupInspection(ActionEvent event) throws IOException {
        Stage stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/neusoft/nepa/view/ProvinceGroupInspectionView.fxml"));
        Parent parent = fxmlLoader.load();
        Scene newScene = new Scene(parent, 960, 540);
        stage.setTitle("NorthEast Software Corporation Public Supervisor Platform Manage System");
        stage.setScene(newScene);
        stage.show();
    }
    public void handleHyperlinkClick(AdminData data) throws IOException {
        Stage stage = new Stage();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/neusoft/nepa/view/AQIConfigDetail.fxml"));
        Parent parent = fxmlLoader.load();
        stage.getProperties().put("userInformation",data);
        Scene newScene = new Scene(parent, 960, 540);
        stage.setTitle("Check for information");
        stage.setResizable(false);
        stage.setScene(newScene);
        stage.show();
    }
}
